/**
 * Created by duwei on 2018/12/1.
 */

var NewCoinShopRecordWindow = Window.extend({
    _ccsFileName: res.NewCoinShopRecordWindow_json,

    _onCCSLoadFinish: function () {
        View.prototype._onCCSLoadFinish.call(this);
        this._addCloseBtn();

        var self = this;
        for (var i = 1; i <= 4; i++) {
            self._nodeList['btn_' + i].setTag(i);
        }

        self.fangkaList = [];
        self.jinbiList = [];
        self.zuanshiList = [];
        self.libaoList = [];
        self.itemList=[];


        showLoading('获取数据中');
        var getCfgUrl = NewCoinShopCfg.NewCoinShopRcordUrl;
        var cfgdata = {
            area: app.area,
            playerid: gameData.uid
        };
        console.log(JSON.stringify(cfgdata));
        http.post(getCfgUrl, cfgdata,
            function (response) {
                hideLoading();
                console.log(JSON.stringify(response));
                self.fangkaList=response.data;
                self.itemList=self.fangkaList;
                self._initTableView();
                self._changeBtn(1);

            }, function (response) {
                hideLoading();
                showAlert('抱歉，系统繁忙！');
            }
        );



        network.send(1077,
            {userid: gameData.uid + '', areaid: NewCoinShopCfg.gameid}
        );

        network.addListener(1078, function (data) {
            cc.log('1078====' + JSON.stringify(data));
            if (data.info) {
                self._initJinBiShuJu(data.info);
            }

        }, this);



    },

    _initJinBiShuJu: function (recordList) {
        var self = this;
        for (var i = 0; i < recordList.length; i++) {
            if (recordList[i]['tagtype'] == 'coin') {
                self.jinbiList.push(recordList[i]);
            }
            else if (recordList[i]['tagtype'] == 'diamond') {
                self.zuanshiList.push(recordList[i]);
            }
            else if (recordList[i]['tagtype'] == 'packet') {
                self.libaoList.push(recordList[i]);
            }
        }
        // cc.log(JSON.stringify(self.jinbiList));
        // cc.log(JSON.stringify(self.zuanshiList));
        // cc.log(JSON.stringify(self.libaoList));
        //
        // if (self.curIndex == 2) {
        //     this.itemList = self.jinbiList;
        // }
        // if (self.curIndex == 3) {
        //     this.itemList = self.zuanshiList;
        // }
        // if (self.curIndex == 4) {
        //     this.itemList = self.libaoList;
        // }

    },

    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'btn_1': '_clickBtn',
            'btn_2': '_clickBtn',
            'btn_3': '_clickBtn',
            'btn_4': '_clickBtn',

        };
    },
    _clickBtn: function (node) {
        this._changeBtn(node.getTag());

    },
    _changeBtn: function (index) {
        var self = this;
        for (var i = 1; i <= 4; i++) {
            if (index == i) {
                self._nodeList['btn_' + i + '.btnForce'].setVisible(true);
            }
            else {
                self._nodeList['btn_' + i + '.btnForce'].setVisible(false);
            }
        }
        self.curIndex = index;
        switch (index) {
            case 1:
                self.itemList = self.fangkaList;
                break;
            case 2:
                self.itemList = self.jinbiList;
                break;
            case 3:
                self.itemList = self.zuanshiList;
                break;
            case 4:
                self.itemList = self.libaoList;
                break;
            default:
                break;
        }
        cc.log('ppp=='+JSON.stringify(self.itemList));
        if (self.tableView) {
            self.tableView.reloadData();
        }

    },

    _initTableView: function () {
        var self = this;
        var tableView = new cc.TableView(self, cc.size(760, 340));
        tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        tableView.x = 60;
        tableView.y = 60;
        tableView.setDelegate(this);
        tableView.setBounceable(true);
        self._nodeList['bg'].addChild(tableView,2);
        self.tableView = tableView;

    },

    /**
     * 列表事件
     * @private
     */
    tableCellTouched: function (_, cell) {
        cc.log(cell.getIdx());
    },
    /**
     * 初始化tableItem
     */
    tableCellAtIndex: function (table, idx) {
        var self = this;
        var cell = table.dequeueCell();
        var node = null;
        cell = new cc.TableViewCell();
        // cc.log(idx, self.itemList[idx]);
        node = addItem('NewCoinShopRecordCellItem', cell, idx, self.itemList[idx],self.curIndex==1);
        node.setName("itemCell");
        return cell;
    },
    /**
     * 初始化tableItem
     */
    tableCellSizeForIndex: function () {
        return cc.size(760, 45);
    },
    /**
     *
     * @return {Number}
     */
    numberOfCellsInTableView: function () {
        return this.itemList.length;
    }


});